package crosshairs;

import common.ChartUtil;
import org.jfree.chart.*;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.panel.CrosshairOverlay;
import org.jfree.chart.plot.Crosshair;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.RectangleAnchor;
import org.jfree.chart.ui.RectangleEdge;
import org.jfree.data.general.DatasetUtils;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Rectangle2D;

public class CrosshairOverlayDemo2 extends ApplicationFrame {

    static class MyDemoPanel extends JPanel implements ChartMouseListener {
        private static final int SERIES_COUNT = 4;
        private ChartPanel chartPanel;
        private Crosshair xCrosshair;
        private Crosshair yCrosshairs[];

        private JFreeChart createChart(XYDataset xyDataset) {
            JFreeChart jFreeChart = ChartFactory.createXYLineChart("CrosshairOverlayDemo2",
                    "X", "Y", xyDataset);
            return jFreeChart;
        }

        private XYDataset createDataset() {
            XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
            for (int i = 0; i < SERIES_COUNT; i++) {
                XYSeries xySeries = new XYSeries((new StringBuilder()).append("S").append(i).toString());
                for (int j = 0; j < 10; j++) {
                    xySeries.add(j, (double) j + Math.random() * 4D);
                }
                xySeriesCollection.addSeries(xySeries);
            }

            return xySeriesCollection;
        }

        @Override
        public void chartMouseClicked(ChartMouseEvent chartMouseEvent) {

        }

        @Override
        public void chartMouseMoved(ChartMouseEvent chartMouseEvent) {
            Rectangle2D rectangle2D = chartPanel.getScreenDataArea();
            JFreeChart jFreeChart = chartMouseEvent.getChart();
            XYPlot xyPlot = (XYPlot) jFreeChart.getPlot();
            ValueAxis valueAxis = xyPlot.getDomainAxis();
            double d = valueAxis.java2DToValue(chartMouseEvent.getTrigger().getX(), rectangle2D, RectangleEdge.BOTTOM);
            xCrosshair.setValue(d);
            for (int i = 0; i < SERIES_COUNT; i++) {
                double d1 = DatasetUtils.findYValue(xyPlot.getDataset(), i, d);
                yCrosshairs[i].setValue(d1);
            }
        }

        public MyDemoPanel() {
            super(new BorderLayout());
            JFreeChart jFreeChart = createChart(createDataset());
            chartPanel = new ChartPanel(jFreeChart);
            chartPanel.addChartMouseListener(this);

            CrosshairOverlay crosshairOverlay = new CrosshairOverlay();
            xCrosshair = new Crosshair((0.0D / 0.0D), Color.GRAY, new BasicStroke(1.0F));
            xCrosshair.setLabelVisible(true);
            crosshairOverlay.addDomainCrosshair(xCrosshair);

            yCrosshairs = new Crosshair[SERIES_COUNT];
            for (int i = 0; i < SERIES_COUNT; i++) {
                yCrosshairs[i] = new Crosshair((0.0D / 0.0D), Color.GRAY, new BasicStroke(1.0F));
                yCrosshairs[i].setLabelVisible(true);
                if (i % 2 != 0) {
                    yCrosshairs[i].setLabelAnchor(RectangleAnchor.TOP_RIGHT);
                }
                crosshairOverlay.addRangeCrosshair(yCrosshairs[i]);
            }

            chartPanel.addOverlay(crosshairOverlay);
            add(chartPanel);
        }
    }

    public CrosshairOverlayDemo2(String title) {
        super(title);
        setContentPane(createDemoPanel());
    }

    private static JPanel createDemoPanel() {
        return new MyDemoPanel();
    }

    public static void main(String[] args) {
        CrosshairOverlayDemo2 demo1 = new CrosshairOverlayDemo2("CrosshairOverlayDemo1");
        ChartUtil.showChartDemo(demo1);
    }
}
